package com.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.management.commons.exception.DefectPriorityException;
import com.management.commons.exception.ExceptionCode.DefectPriorityExceptionCode;
import com.management.controller.form.AddPriorityForm;
import com.management.controller.form.ChangePriorityForm;
import com.management.controller.form.GetAllPriorityForm;
import com.management.controller.form.SelPriorityByNameForm;
import com.management.controller.info.GetAllPriorityInfo;
import com.management.controller.info.SelPriorityByNameInfo;
import com.management.model.DefectPriority;
import com.management.dao.DefectPriorityDao;
import com.management.service.DefectPriorityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.management.utils.CopyUtils;
import com.management.utils.Result;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 龙世超
 * @since 2022-03-23
 */
@Service
public class DefectPriorityServiceImpl extends ServiceImpl<DefectPriorityDao, DefectPriority> implements DefectPriorityService {

    @Override
    public Result findAllPriority(GetAllPriorityForm getAllPriorityForm) {
        GetAllPriorityForm page = page(getAllPriorityForm);
        List<GetAllPriorityInfo> list = CopyUtils.CopyList(page.getRecords(), GetAllPriorityInfo.class);
        Page<GetAllPriorityInfo> infoPage = new Page<>();
        infoPage.setRecords(list);
        infoPage.setSize(page.getSize());
        infoPage.setTotal(page.getTotal());
        infoPage.setCurrent(page.getCurrent());
        infoPage.setPages(page.getPages());
        return Result.getInstance(Page.class).setData(infoPage);
    }

    @Override
    public Result addPriority(AddPriorityForm addPriorityForm) {
        QueryWrapper<DefectPriority> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("priority_desc",addPriorityForm.getPriorityName());
        DefectPriority one = getOne(queryWrapper);
        if(one!=null){
            throw new DefectPriorityException(DefectPriorityExceptionCode.PRIORITYDESC_EXISTS_ERROR);
        }
        if(addPriorityForm.getPriorityName()==null||addPriorityForm.getPriorityName().equals("")){
            return Result.getInstance().setCode(601).setMessage("请输入内容");
        }
        DefectPriority defectPriority = new DefectPriority();
        defectPriority.setPriorityDesc(addPriorityForm.getPriorityName());
        defectPriority.setCreateBy(addPriorityForm.getAccountName());
        defectPriority.setModifyBy(addPriorityForm.getAccountName());
        boolean save = save(defectPriority);
        if (save) {
            return Result.getInstance().setCode(200).setMessage("添加成功");
        }
        throw new DefectPriorityException(DefectPriorityExceptionCode.ADDPRIORITY_ERROR);
    }

    @Override
    public Result closePriority(Integer priorityId) {
        DefectPriority byId = getById(priorityId);
        if (byId.getModuleState().equals("有效")) {
            byId.setModuleState("禁用");
            saveOrUpdate(byId);
        } else {
            byId.setModuleState("有效");
            saveOrUpdate(byId);

        }

        return Result.getInstance().setCode(200).setMessage("操作成功");
    }

    @Override
    public Result checkPriorityState(Integer priorityId) {
        DefectPriority byId = getById(priorityId);
        if (byId.getModuleState().equals("有效")){
            return Result.getInstance().setCode(200);
        }
        throw new DefectPriorityException(DefectPriorityExceptionCode.PRIORITYSTATE_ERROR);
    }

    @Override
    public Result delPriority(Integer priorityId) {
        boolean b = removeById(priorityId);
        if(b){
            return Result.getInstance().setCode(200).setMessage("操作成功");
        }
       throw new DefectPriorityException(DefectPriorityExceptionCode.DELPRIORITY_ERROR);
    }

    @Override
    public Result findPriorityByName(SelPriorityByNameForm selPriorityByNameForm) {
        QueryWrapper<DefectPriority> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("priority_desc",selPriorityByNameForm.getPriorityName());
        SelPriorityByNameForm page = page(selPriorityByNameForm, queryWrapper);
        List<SelPriorityByNameInfo> list = CopyUtils.CopyList(page.getRecords(), SelPriorityByNameInfo.class);
        Page<SelPriorityByNameInfo> page1=new Page<>();
        page1.setRecords(list);
        page1.setPages(page.getPages());
        page1.setCurrent(page.getCurrent());
        page1.setSize(page.getSize());
        page1.setTotal(page.getTotal());
        return Result.getInstance(Page.class).setData(page1);
    }

    @Override
    public Result changePriority(ChangePriorityForm changePriorityForm) {
        QueryWrapper<DefectPriority> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("priority_desc",changePriorityForm.getPriorityName());
        DefectPriority one = getOne(queryWrapper);
        if(one!=null){
            throw new DefectPriorityException(DefectPriorityExceptionCode.CHANGEPRIORITY2_ERROR);
        }
        if(changePriorityForm.getPriorityName()==null||changePriorityForm.getPriorityName().equals("")){
            return Result.getInstance().setCode(600).setMessage("请输入内容");
        }
        DefectPriority byId = getById(changePriorityForm.getPriorityId());
        byId.setPriorityDesc(changePriorityForm.getPriorityName());
        byId.setModifyBy(changePriorityForm.getAccountName());
        boolean b = saveOrUpdate(byId);
        if(b){
            return Result.getInstance().setCode(200).setMessage("修改成功");
        }
        throw new DefectPriorityException(DefectPriorityExceptionCode.CHANGEPRIORITY_ERROR);
    }

    @Override
    public Result getAllPrioritys() {
        List<DefectPriority> list = list();
        return Result.getInstanceList(DefectPriority.class).setData(list);
    }
}